﻿using Humanizer;
using Microsoft.AspNetCore.Mvc;
using MVC_System.DTO.RawMaterial;
using MVC_System.DTO.Storage;
using Newtonsoft.Json;
using WMS_MAV;
using static System.Net.WebRequestMethods;

namespace MVC_System.Controllers
{
    public class StoragesController : Controller
    {
		private readonly IConfiguration _configuration;
		public StoragesController(IConfiguration configuration)
		{
			_configuration = configuration;
		}
		public IActionResult StorageShow()
        {
            return View();
        }
        public IActionResult StorageAdd()
        {
            return View();
        }
		public IActionResult StorageUpdate()
        {
            return View();
        }
		public IActionResult StorageDetail()
        {
            return View();
        }
		//显示
        public async Task<ActionResult> StoragesShow(StorageShowDTO dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "RawApi/api/Storages/StoragesShow?PageIndex=" + dto.PageIndex+ "&PageSize=" +dto.PageSize+ "&StorageNumbering="+dto.StorageNumbering+ "&DetailsNumbering="+dto.DetailsNumbering+ "&Status="+dto.Status;
            var respomse = await  client.GetAsync(url);
            return Json(respomse);
        }
		//一级
		public async Task<ActionResult> warehouseList(string Token)
		{
			HttpClientWrapper client = new HttpClientWrapper(Token);
			string url = "http://localhost:5076/api/Warehouse/WarehouseList";
			var respomse = await client.GetAsync(url);
			return Json(respomse);
		}
		//二级
		public async Task<ActionResult> reservoirNameList(string id,string Token)
		{
			HttpClientWrapper client = new HttpClientWrapper(Token);
			string url = "http://localhost:5035/api/Values/ShowSelectReservoir?WarehouseID="+id;
			var respomse = await client.GetAsync(url);
			return Json(respomse);
		}
		//三级
		public async Task<ActionResult> locationNameList(string id, string Token)
		{
			HttpClientWrapper client = new HttpClientWrapper(Token);
			string url = "http://localhost:5035/api/Location/GetSelectLocation?ReservoirId=" + id;
			var respomse = await client.GetAsync(url);
			return Json(respomse);
		}
		//添加
		public async Task<ActionResult> StoragesAdd(StorageAddDTo dto)
		{
			HttpClientWrapper client = new HttpClientWrapper(dto.Token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StorageAdd";
			var respomse = await client.PostAsync(url,dto.Data);
			return Json(respomse);
		}
		//删除
		public async Task<ActionResult> StoragesDelete(StorageDeleteDTO dto, string token)
		{
			HttpClientWrapper client = new HttpClientWrapper(token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StorageDelete";
			string list = JsonConvert.SerializeObject(dto);
			var respomse = await client.PostAsync(url, list);
			return Json(respomse);
		}
		//批量删除
		public async Task<ActionResult> StoragesDeletePi(StorageDeletePiDTO dto, string token)
		{
			HttpClientWrapper client = new HttpClientWrapper(token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StorageDeletePi";
			string list = JsonConvert.SerializeObject(dto);
			var respomse = await client.PostAsync(url, list);
			return Json(respomse);
		}
		//反填
		public async Task<ActionResult> StoragesMondel(StorageMondelDTO dto)
		{
			HttpClientWrapper client = new HttpClientWrapper(dto.Token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StoragesMondel?id=" + dto.id;
			var respomse = await client.GetAsync(url);
			return Json(respomse);
		}
		//修改
		public async Task<ActionResult> StoragesUpdate(StoragesUpdateDTO dto, string token)
		{
			HttpClientWrapper client = new HttpClientWrapper(token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StorageUpdate";
			string list = JsonConvert.SerializeObject(dto);
			var respomse = await client.PostAsync(url, list);
			return Json(respomse);
		}
		//修改
		public async Task<ActionResult> StoragesUpdateStatus(StoragesUpdateStatusDTO dto, string token)
		{
			HttpClientWrapper client = new HttpClientWrapper(token);
			string url = _configuration["Url"] + "RawApi/api/Storages/StorageUpdateStatus?id=" +dto.id;
			string list = JsonConvert.SerializeObject(dto);
			var respomse = await client.PostAsync(url, list);
			return Json(respomse);
		}

        //更新原料库存数量
        public async Task<ActionResult> StoragesUpdateInventory(StoragesUpdateStatusDTO dto, string token)
        {
            HttpClientWrapper client = new HttpClientWrapper(token);
			string url = "http://localhost:5093/api/RawMaterials/UpdateInventory?StorageId=" + dto.id;
				//_configuration["Url"] + "RawApi/api/UpdateInventory/UpdateInventory?StorageId=" + dto.id;
            string list = JsonConvert.SerializeObject(dto);
            var respomse = await client.PostAsync(url, list);
            return Json(respomse);
        }
    }
}
